//
//  DetailsViewController.m
//  AirConditioner
//
//  Created by Sean Wang on 12-4-21.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DetailsViewController.h"
#import "OrderOnlineViewController.h"
#import "WhereToBuyViewController.h"
#import "RootViewController.h"

@interface DetailsViewController ()

@end

@implementation DetailsViewController

@synthesize myWebView, features, specs, whereToBuy;
@synthesize whereToBuyView, findSpecialistLabel, scrollView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"NCG Aircon";
        
        UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
        temporaryBarButtonItem.title = @"Back";
        self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
        
        NSString* path = [[NSBundle mainBundle] pathForResource:@"bulletList" 
                                                         ofType:@"html"];
        htmlStringFeatures = [NSString stringWithContentsOfFile:path
                                                    encoding:NSUTF8StringEncoding
                                                       error:NULL];
    }
    
    return self;
}
/*
- (void)loadView 
{
    [super loadView];
    scrollView = [[UIScrollView alloc] initWithFrame:[[self view] bounds]];
    [[self view] addSubview:scrollView];
    UIBarButtonItem *infoItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"segment_tools.png"]
                                                                 style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(openBrowser)];
    self.navigationItem.rightBarButtonItem = infoItem;
    
}
*/
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIBarButtonItem *infoItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"segment_tools.png"]
                                                                 style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(openBrowser)];
    self.navigationItem.rightBarButtonItem = infoItem;

	self.myWebView.backgroundColor = [UIColor blackColor];
	//self.myWebView.scalesPageToFit = YES;
	self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    self.myWebView.scrollView.scrollEnabled = NO;
    
    
    
    //NSLog(@"myWebView.scrollView.contentSize:%f",self.myWebView.scrollView.contentSize.height);
    //NSLog(@"viewDidLoad,self.view.bounds.origin.y:%f",self.view.bounds.origin.y);
	[self.myWebView loadHTMLString:htmlStringFeatures baseURL:nil];
    currentView = self.features;
    //NSLog(@"myWebView.scrollView.contentSize:%f",self.myWebView.scrollView.contentSize.height);
    //NSLog(@"viewDidLoad,self.view.bounds.origin.y:%f",self.view.bounds.origin.y);

}

- (void)viewDidAppear:(BOOL)animated
{
    if(htmlStringSpecs == nil){
        NSString* path = [[NSBundle mainBundle] pathForResource:@"gridView" 
                                                         ofType:@"html"];
        htmlStringSpecs = [NSString stringWithContentsOfFile:path
                                                      encoding:NSUTF8StringEncoding
                                                         error:NULL];
    }
}

- (void) openBrowser
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.panasonic.com.au/airconditioning"]];
}

- (void)setButtonHighlighted:(UIButton*)button
{
    button.highlighted = YES;
    button.enabled = NO;
}

- (IBAction)tabViewChanged:(id)sender
{
    if (sender != currentView) {
        UIButton *newView = (UIButton*)sender;
        [self performSelector:@selector(setButtonHighlighted:) withObject:newView afterDelay:0];
        //newView.highlighted = YES;
        currentView.highlighted = NO;
        currentView.enabled = YES;
        
        CGRect webFrame = self.myWebView.frame;
        webFrame.size.height = 146;  
        self.myWebView.frame = webFrame;
        
        CGRect bounds = scrollView.bounds;
        scrollView.contentSize = bounds.size;
        
        if (newView == self.features) {
            self.whereToBuyView.hidden = YES;
            self.myWebView.hidden = NO;
            [self.myWebView loadHTMLString:htmlStringFeatures baseURL:nil];
        }else if(newView == self.specs){
            self.whereToBuyView.hidden = YES;
            self.myWebView.hidden = NO;
            [self.myWebView loadHTMLString:htmlStringSpecs baseURL:nil];
        }else if(newView == self.whereToBuy){
            self.whereToBuyView.hidden = NO;
            self.myWebView.hidden = YES;
        }
        
        currentView = newView;
    }
}

- (IBAction)findSites:(id)sender
{
    UIButton *button = (UIButton*)sender;
    WhereToBuyViewController *whereToBuyViewController = [RootViewController getWhereToBuyViewController];
    UINavigationController *navigationViewController = whereToBuyViewController.navigationController;
    if(navigationViewController.viewControllers.count > 1){        
        [navigationViewController popToRootViewControllerAnimated:NO];
    }
    
    [whereToBuyViewController navigateToFindSitesPage:button.tag];
    [self.tabBarController setSelectedIndex:2];
    
}

- (IBAction)cusomerCare:(id)sender   //Order online
{
    if(orderOnlineViewController == nil){
        orderOnlineViewController = [[OrderOnlineViewController alloc]
                                 initWithNibName:@"OrderOnlineViewController" bundle:nil];
    }
    
    orderOnlineViewController.detailsViewController = self;
    
    [[self navigationController] pushViewController:orderOnlineViewController animated:YES];
}

- (void)moveToDetailsPage
{
    [[self navigationController] dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark UIWebViewDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView
{
	// starting the load, show the activity indicator in the status bar
	
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
	//NSLog(@"webViewDidFinishLoad,myWebView.scrollView.contentSize:%f",self.myWebView.scrollView.contentSize.height);
    //NSLog(@"webViewDidFinishLoad,self.myWebView.bounds.origin.y:%f",self.myWebView.bounds.origin.y);
    //NSLog(@"webViewDidFinishLoad,self.view.bounds.origin.y:%f",self.view.bounds.origin.y);
    CGRect webFrame = self.myWebView.frame;
    CGFloat oldHeight = webFrame.size.height;
    CGFloat newHeight = self.myWebView.scrollView.contentSize.height;
    
    //NSLog(@"oldHeight=%f", oldHeight);
    //NSLog(@"newHeight=%f", newHeight);
    
    CGRect scrollFrame = self.scrollView.frame;
    
    if(newHeight != oldHeight){                
        self.scrollView.contentSize = CGSizeMake(scrollFrame.size.width, scrollFrame.size.height+newHeight-oldHeight);
        webFrame.size.height = newHeight;  
        self.myWebView.frame = webFrame;
    }
    	
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{    
	// report the error inside the webview
	NSString* errorString = [NSString stringWithFormat:
							 @"<html><center><font size=+5 color='red'>An error occurred:<br>%@</font></center></html>",
							 error.localizedDescription];
	[self.myWebView loadHTMLString:errorString baseURL:nil];
}

@end
